﻿Public Class frmInputCoinType
    Private khrDa As New DSVaultingTableAdapters.khr_coinsTableAdapter
    Private khrDt As New DataTable
    Private action As String
    Private actType As String() = {"NONE", "INSERT", "UPDATE", "DELETE"}
    Private slcVal As Long

    Private Sub updateDGV()
        uxCoinTypedgv.DataSource = khrDa.GetData
    End Sub

    Private Sub frmInputCoinType_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmSetting.Enabled = True
    End Sub

    Private Sub frmInputCoinType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Fill dataset into Data Grid View
        khrDt = khrDa.GetData
        Me.updateDGV()
    End Sub

    Private Sub uxInputNewCoinbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxInputNewCoinbtn.Click
        uxInputCoinTypetbx.Enabled = True
        uxInputCoinTypetbx.Text = ""
        action = actType(1)
    End Sub

    Private Sub uxEditCoinbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxEditCoinbtn.Click
        If uxCoinTypedgv.SelectedRows.Count > 0 Then
            uxInputCoinTypetbx.Enabled = True
            uxInputCoinTypetbx.Text = uxCoinTypedgv.SelectedRows.Item(0).Cells(1).Value
            If Not uxInputCoinTypetbx.Text = "" Then
                slcVal = uxInputCoinTypetbx.Text
                action = actType(2)
            End If
        End If
    End Sub

    Private Sub uxDeleteCoinbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxDeleteCoinbtn.Click
        If MessageBox.Show("តើអ្នកពិតជាចង់លុបចោលទិន្នន័យនេះមែនទេ?", "សារបញ្ជាក់ពីការលុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
            Dim oldId As Long = uxCoinTypedgv.SelectedRows.Item(0).Cells(0).Value
            khrDa.Delete(oldId, uxCoinTypedgv.SelectedRows.Item(0).Cells(1).Value)
            Me.updateDGV()
        End If
    End Sub

    Private Sub uxInputCoinSavebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxInputCoinSavebtn.Click
        Dim noteVal As Integer = If(uxInputCoinTypetbx.Text = "", 0, uxInputCoinTypetbx.Text)
        If noteVal > 0 And action = actType(1) Then
            khrDa.Insert(noteVal)
            Me.updateDGV()
            uxInputCoinTypetbx.Clear()
        ElseIf noteVal > 0 And action = actType(2) Then
            If MessageBox.Show("តើអ្នកពិតជាចងរក្សាទុកព័ត៌មានដែលបានកែប្រែមែនទេ?", "សារបញ្ជាក់ពីការកែប្រែ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                Dim oldId As Long = uxCoinTypedgv.SelectedRows.Item(0).Cells(0).Value
                khrDa.Update(noteVal, oldId, slcVal)
                Me.updateDGV()
            End If
        Else
            MsgBox("Please input some value.")
        End If
    End Sub

    Private Sub uxCancelbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxCancelbtn.Click
        frmSetting.Enabled = True
        Me.Close()
    End Sub

    Private Sub uxCoinTypedgv_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles uxCoinTypedgv.CellDoubleClick
        uxEditCoinbtn_Click(sender, e)
    End Sub

    Private Sub uxInputCoinTypetbx_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles uxInputCoinTypetbx.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            uxInputCoinSavebtn_Click(sender, e)
        End If
    End Sub
End Class